const app = getApp()
const WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productid: null,
    isFocus: false,
    buyNow: false,
    productinf: {},
    proSwiper: ['../image/img1.jpg', '../image/img2.jpg'],
    currentIdx: 1,
    ImgLth: 1,
    num: 1,
    showTip: false,
    detail:'',








  },
  buyNow: function() {
    var that = this
    // wx.showLoading({
    //   title: '加载中',
    // })

    setTimeout(function() {
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease'
      });
      animation.translate(0, -220).step()
      that.setData({
        buyNow: true,
      })
      setTimeout(function() {
        that.setData({
          ani: animation.export()
        })
      }, 1)
      wx.hideLoading()
    }, 1000)
  },
  // 加入购物车
  showTip: function() {
    var that = this
    // wx.showLoading({
    //   title: '加载中',
    // })

    setTimeout(function() {
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease'
      });
      animation.translate(0, -220).step()
      that.setData({
        showTip: true,
      })
      setTimeout(function() {
        that.setData({
          ani: animation.export()
        })
      }, 1)
      wx.hideLoading()
    }, 1000)
  },
  closeTip: function(e) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        showTip: false,
        ani: animation.export()
      })
    }
       this.setData({
        num: this.data.num
      })

  },
  closeBuyNow: function(e) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        buyNow: false,
        ani: animation.export()
      })
    }

  },


  swiper(e) {
    this.setData({
      currentIdx: e.detail.current + 1
    })
  },
  previewImage: function(e) {
    wx.previewImage({ //只能网络地址
      current: e.currentTarget.src,
      urls: this.data.proSwiper
    })
  },
  // 点击首页
  show: function() {
    wx.switchTab({
      url: '/pages/index/index',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 点击进店 
  comeroom: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  focus: function() {
    var that = this
    app.req({
      url: 'User/Collect?productid=' + this.data.productid,
      method: "POST",
      success: function(res) {
        if (res.data.Code == true) {
          that.setData({
            isFocus: true
          })
          wx.showToast({
            title: '收藏成功',
            icon: 'success'
          })
        }
      }
    })

  },
  unfocus: function() {
    var that = this
    app.req({
      url: 'User/Collect/' + this.data.productid,
      method: 'POST',
      success: function(res) {
        if (res.data.Code == true) {
          that.setData({
            isFocus: false
          })
          wx.showToast({
            title: '取消收藏',
            icon: 'succcess'
          })
        }
      }
    })
  },
  // 商品详情
  getList: function() {
    var that = this
    app.req({
      url: "Product/Product/" + this.data.productid,
      success: function(res) {
        var obj = res.data.Result
        if (res.data.Result.Product.IsCollect == true) {
          that.setData({
            isFocus: true
          })
        }
        var imgs = []
        for (let i in obj.Product.ProductImages){
          imgs.push(app.globalData.img+obj.Product.ProductImages[i].Url)
        }
        var str = obj.Detail
        var arr = str.split("/Upload/File/")
        for(let i=0;i<arr.length;i++){
          if(i < arr.length-1){
            // console.log(arr[i] + app.globalData.img)
            arr[i] = arr[i]+app.globalData.img
          }
        }
        
        var mess = arr.join("/Upload/File/")
        // delete obj.Detail

        // console.log(res.data.Result)
        // console.log(res.data.Result.Product.IsCollect)

        that.setData({
          productinf: res.data.Result,
          isFocus: res.data.Result.Product.IsCollect,
          proSwiper: imgs,
          ImgLth: imgs.length,
          detail: mess
        })
      }
    })
  },
  //  购物车减
  sub: function() {
    if (this.data.num <= 1) {
      this.setData({
        num: 1
      })
    } else {
      this.setData({
        num: this.data.num - 1
      })
    }
  },

  // 购物车加
  add:function () {
    this.setData({
      num: this.data.num + 1
    })
  },
  surePush: function (e) {
    console.log(e)
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        showTip: true,
        ani: animation.export()
      }),
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    var that = this
    // console.log(options.id)
    this.setData({
      productid: options.id
    })
    // console.log(app.globalData.Authorization)
    this.getList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // this.unfocus()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {
  //     return{
  //       title:'商品详情',
  //       path: '/pages/prodetail/prodetail?share=1&&id=' + this.options.id,
  //       success:function(res) {}
        
  //     }
  // }
  onShareAppMessage: function () {
    console.log(12)
    let that = this;
    return {
      title: '商品详情', // 转发后 所显示的title
      path: '/pages/prodetail/prodetail?share=1&&id=' + this.options.id, // 相对的路径
      success: (res) => {    // 成功后要做的事情
        console.log(res.shareTickets[0])
        wx.getShareInfo({
          shareTicket: res.shareTickets[0],
          success: (res) => {
            that.setData({
              isShow: true
            })
            console.log(that.setData.isShow)
          },
          fail: function (res) { console.log(res) },
          complete: function (res) { console.log(res) }
        })
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    }
  },
})